import {ecs} from "db://oops-framework/libs/ecs/ECS";
import {MjEntity} from "db://assets/script/game/majiang/entity";
import {MjDeskView} from "db://assets/script/game/majiang/view/mjDeskView";
import {oops} from "db://oops-framework/core/Oops";
import {UIID} from "db://assets/script/game/common/config/gameUiConfig";
import {smc} from "db://assets/script/game/common/singletonModuleComp";
import {InitMjDeskComp} from "db://assets/script/game/majiang/comp/initMjDeskComp";
import { pb } from "../../../network/ws/proto";
import {Player} from "db://assets/script/game/player/player";
import {pt} from "db://assets/script/game/player/api";

// 房主创建新房间 or 用户加入新房间
export class CreateMjDeskSystem extends ecs.ComblockSystem implements ecs.IEntityEnterSystem {
    filter(): ecs.IMatcher {
        return ecs.allOf(InitMjDeskComp)
    }

    entityEnter(entity: MjEntity) {
        const comp = entity.get(InitMjDeskComp)
        const destroyDesk = () => {
            if (comp.onFinish) {
                // 处理剩下的回调函数
                comp.onFinish()
            }
            entity.remove(InitMjDeskComp)
        }
        smc.dating.hide()
        if (comp.reconnect && oops.gui.has(UIID.createMjDesk) ){
            // 重连，房间什么的都在
            smc.gameMj.initDesk(comp.deskInfo, comp.playerMap())
            smc.gameMj.setCategoryConf(comp.categoryConf)
            smc.gameMj.updatePlayerCard(comp.playerCards)
            // 更新房间信息
            smc.gameMj.updateRoundInfo(comp.roundInfo)
            const deskView = entity.get(MjDeskView)
            this.updatePlayerView(deskView)
            // 检查是否开始游戏
            smc.gameMj.addStartDeskComp(comp.newTile)
            destroyDesk()
        } else {
            oops.gui.openAsync(UIID.createMjDesk).then(node => {
                smc.gameMj.initDesk(comp.deskInfo, comp.playerMap())
                smc.gameMj.updatePlayerCard(comp.playerCards)
                smc.gameMj.updateRoundInfo(comp.roundInfo)
                // 更新房间配置
                smc.gameMj.setCategoryConf(comp.categoryConf)
                if (smc.gameMj.deskInfo.mjOpt.isRuby) {
                    // 监听金豆房
                    smc.gameMj.addListenComp()
                }
                const deskView = node.getComponent(MjDeskView)
                entity.add(deskView)
                // 更新规则
                deskView.updateOptShow()
                // 更新玩家信息
                this.updatePlayerView(deskView)
                // 检查是否开始游戏
                if (comp.deskInfo.status !== pt.DeskStatus.created) {
                    smc.gameMj.addStartDeskComp(comp.newTile)
                }
                destroyDesk()
            })
        }
    }

    updatePlayerView(deskView: MjDeskView) {
        const myShortId = Player.shortId()
        Player.getInstance().model = smc.gameMj.playerInfos[myShortId].playerInfo
        const playerView = deskView.getPlayerView()
        const playerList = smc.gameMj.getPlayerOrder()
        let playerInfo: pb.IPlayerModel
        let playDeskInfo: pb.IPlayerDeskInfo
        deskView.resetPlayerCounter()
        for (let i = 0; i < playerView.sideOrder.length; i++) {
            if (playerList[i]) {
                playerInfo = playerList[i].playerInfo
                playDeskInfo = playerList[i].playerDeskInfo
                playerView.initPlayerInfo(i, playerInfo.avatar, playerInfo.nickname, Number(playerInfo.ruby),
                    Number(playerInfo.shortId), playDeskInfo.isReady)
                if (playDeskInfo.isReady) {
                    // 已准备好
                    // 不显示准备按钮
                    if (playerInfo.shortId === myShortId) {
                        deskView.playerReady()
                    }
                    playerView.toggleReadyHandByShortId(playerInfo.shortId, true)
                }
                // 绑定玩家方位
                deskView.bindCounterByShortId(Number(playerInfo.shortId), i)
            } else {
                // 还没人，先隐藏
                playerView.togglePlayerByIndex(i, false)
            }
        }
    }
}